﻿using System.Collections.Generic;
using System.Xml;
using ApiBetaSeries.Model;
using ApiBetaSeries.Utils;

namespace ApiBetaSeries.Factories
{
    public class EpisodeFactory
    {
        /// <summary>
        /// Create an Episode from an Xml node
        /// </summary>
        /// <param name="e">Xml node</param>
        /// <returns>Episode</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "shows/episodes", "planning/member", "members/episodes", "planning/general", "subtitles/show")]
        public static IEpisode CreateEpisode(XmlNode e)
        {
            IEpisode episode = new Episode();
            episode.Show = UtilsXML.GetStringValue(e, "show");
            episode.UrlShow = UtilsXML.GetStringValue(e, "url");
            episode.Season = UtilsXML.GetIntValue(e, "season");
            episode.Number = UtilsXML.GetIntValue(e, "episode");
            episode.NumberFormat = UtilsXML.GetStringValue(e, "number");

            // if number is null, episode is number and we calc season&episode from it
            if (string.IsNullOrEmpty(episode.NumberFormat))
            {
                episode.NumberFormat = UtilsXML.GetStringValue(e, "episode");

            }

            if (!string.IsNullOrEmpty(episode.NumberFormat) && (episode.Season < 0 || episode.Number < 0))
            {
                episode.Season = UtilsXML.GetSeasonFromFormat(episode.NumberFormat);
                episode.Number = UtilsXML.GetEpisodeFromFormat(episode.NumberFormat);
            }

            episode.Date = UtilsXML.GetDateValue(e, "date");
            episode.Title = UtilsXML.GetStringValue(e, "title");
            episode.Description = UtilsXML.GetStringValue(e, "description");
            episode.Screen = UtilsXML.GetStringValue(e, "screen");
            episode.HasSeen = UtilsXML.GetBoolValue(e, "has_seen");
            episode.Downloaded = UtilsXML.GetBoolValue(e, "downloaded");


            if (e["note"] != null)
            {
                episode.NbMark = UtilsXML.GetIntValue(e["note"], "members");
                episode.AverageMark = UtilsXML.GetDoubleValue(e["note"], "mean");
                episode.Mark = UtilsXML.GetIntValue(e["note"], "self");
            }
            if (e["subs"] != null)
            {
                // Add the subtitles
                episode.Subtitles = new List<ISubtitle>();
                foreach (XmlElement sub in e["subs"].GetElementsByTagName("sub"))
                {
                    ISubtitle subtitle = SubtitleFactory.CreateSubtitle(sub);
                    subtitle.Show = episode.Show;
                    subtitle.Season = episode.Season;
                    subtitle.Episode = episode.Number;
                    episode.Subtitles.Add(subtitle);
                }

            }
            else
            {
                episode.Subtitles = null;
            }

            return episode;
        }

        /// <summary>
        /// Create an Episode
        /// </summary>
        /// <returns>Episode</returns>
        public static IEpisode CreateEpisode()
        {
            return new Episode();
        }
    }
}
